var app = getApp()
const order = ['demo1', 'demo2', 'demo3']
const db = wx.cloud.database()  //创建数据库实例
Page({

  data: {
    scan: "",
    scrollH: 0,
    imgWidth: 0,
    loadingCount: 0,
    images: [],     //数据
    col1: [],     //左边
    col2: [],     //右边
    avatarUrl: 'images/wx.png',
    userInfo: {}  //用户信息
  },
  return:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  onLoad: function () {
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              this.setData({
                avatarUrl: res.userInfo.avatarUrl,
                userInfo: res.userInfo,
              })
            }
          })
        }
      }
    })
    wx.getSystemInfo({
      success: (res) => {
        let ww = res.windowWidth;
        let wh = res.windowHeight;
        let imgWidth = ww * 0.48;
        let scrollH = wh;

        this.setData({
          scrollH: scrollH,
          imgWidth: imgWidth
        });

        //加载首组图片
        //this.a();
      }
    })
  },
  a: function () {
    let images;
    var that = this;
    let s = this.data.scan;
    db.collection('Bowman').where({  //使用正则查询，实现对搜索的模糊查询

      title: db.RegExp({
        regexp: s
      })

    }).get({
        success: function (res) {
        for (let i = 0; i < res.data.length; i++) {
          var id = "images[" + i + "].id"
          var title = "images[" + i + "].title"
          var nickName = "images[" + i + "].nickName"
          var avatarUrl = "images[" + i + "].avatarUrl"
          var imageurl = "images[" + i + "].imageurl"

          that.setData({
            [id]: res.data[i]._id,
            [title]: res.data[i].title,
            [nickName]: res.data[i].nickName,
            [avatarUrl]: res.data[i].avatarUrl,
            [imageurl]: res.data[i].imageurl[0]
          })
        }
      }
    })
    images = this.data.images;
   
    // let baseId = "img-" + (+new Date());

    // for (let i = 0; i < images.length; i++) {
    //   images[i].id = baseId + "-" + i;
    // }

    this.setData({
      loadingCount: images.length,
      images: images
    });
  },
  aa: function (e) {
    console.log(e.detail.value)
    this.setData({
      scan: e.detail.value
    })
  },

  onImageLoad: function (e) {

    let imageId = e.currentTarget.id;
    let oImgW = e.detail.width;         //图片原始宽度
    let oImgH = e.detail.height;        //图片原始高度
    let imgWidth = this.data.imgWidth;  //图片设置的宽度
    let scale = imgWidth / oImgW;        //比例计算
    let imgHeight = oImgH * scale;      //自适应高度

    let images = this.data.images;
    let imageObj = null;



    for (let i = 0; i < images.length; i++) {
      let img = images[i];
      if (img.id === imageId) {
        imageObj = img;
        break;
      }
    }
    imageObj.height = 100;
    imageObj.height = imgHeight;

    let loadingCount = this.data.loadingCount - 1;
    let col1 = this.data.col1;
    let col2 = this.data.col2;

    //判断当前图片添加到左列还是右列

    if (col1.length <= col2.length) {
      col1.push(imageObj);
    } else {
      col2.push(imageObj);
    }

    let data = {
      loadingCount: loadingCount,
      col1: col1,
      col2: col2

    };

    //当前这组图片已加载完毕，则清空图片临时加载区域的内容
    if (!loadingCount) {
      data.images = [];
    }

    this.setData(data);
  },

  loadImages: function () {
    // let images;

    // var that = this
    // db.collection('Bowman').get({
    //   success: function (res) {


    //     for (let i = 0; i < res.data.length; i++) {
    //       var id = "images[" + i + "].id"
    //       var title = "images[" + i + "].title"
    //       var nickName = "images[" + i + "].nickName"
    //       var avatarUrl = "images[" + i + "].avatarUrl"
    //       var imageurl = "images[" + i + "].imageurl"

    //       that.setData({
    //         [id]: res.data[i]._id,
    //         [title]: res.data[i].title,
    //         [nickName]: res.data[i].nickName,
    //         [avatarUrl]: res.data[i].avatarUrl,
    //         [imageurl]: res.data[i].imageurl[0]
    //       })

    //     }
    //   }
    // })
    images = that.data.images
    console.log(images);
    let baseId = "img-" + (+new Date());

    for (let i = 0; i < images.length; i++) {
      images[i].id = baseId + "-" + i;
    }

    this.setData({
      loadingCount: images.length,
      images: images
    });
  }

})